package sipus.delivery.email.sender;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Caption;
import org.zkoss.zul.Center;
import org.zkoss.zul.Column;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Groupbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.South;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

import sipus.common.Common;
import sipus.database.hibernate.HibernateUtil;
import sipus.database.model.Tbmuser;


public class MailHelper extends GenericAutowireComposer {

	private static final long serialVersionUID = -2744627984975080227L;

	private Textbox usernameInput;

	// private Proposal tglseminar;

	public void tampil(final Window window) {

		Common.clear(window);
		window.getFellowIfAny("window");
		window.setWidth("700px");
		window.setHeight("240px");

		Borderlayout borderlayout = new Borderlayout();
		borderlayout.setParent(window);
		borderlayout.setStyle("border:0px;");
		borderlayout.setWidth("100%");
		borderlayout.setHeight("100%");

		Center center = new Center();
		center.setParent(borderlayout);
		center.setFlex(true);

		Groupbox groupbox = new Groupbox();
		groupbox.setParent(center);
		groupbox.appendChild(new Caption(
				"Masukkan username anda, sistem secara otomatis akan mengirimkan password ke email anda"));
		groupbox.setWidth("100%");
		groupbox.setHeight("100%");

		Grid grid = new Grid();
		grid.setParent(groupbox);
		grid.setWidth("100%");
		grid.setHeight("100%");
		// grid.setOddRowSclass("non-odd");
		grid.setStyle("border:0px;background: transparent;");

		org.zkoss.zul.Columns columns = new org.zkoss.zul.Columns();
		columns.setParent(grid);
		Column column = new Column();
		column.setWidth("30%");
		columns.appendChild(column);
		column = new Column();
		column.setWidth("70%");
		columns.appendChild(column);

		Rows rows = new Rows();
		rows.setParent(grid);

		Row row = new Row();

		row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Id Pengguna"));
		row.appendChild(usernameInput = new Textbox());
		usernameInput.setCols(40);

		South south = new South();
		south.setParent(borderlayout);
		south.setFlex(true);

		Toolbar toolbar = new Toolbar();
		toolbar.setParent(south);
		toolbar.setHeight("40px");
		Toolbarbutton button = new Toolbarbutton("Tutup", "/img/cancel.gif");
		button.setTooltiptext("Tutup");
		button.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				window.setVisible(false);
			}
		});
		button.setParent(toolbar);

		Toolbarbutton save = new Toolbarbutton("Kirim", "/img/sendmail.png");
		save.setTooltiptext("Kirim");
		save.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				// onSave(event);

				sendAndLoad(usernameInput.getValue());
				window.setVisible(false);
			}
		});
		save.setParent(toolbar);

		window.setVisible(true);
		try {
			window.onModal();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void sendAndLoad(String username) throws Exception {

		Session session = HibernateUtil.currentSession();
		Tbmuser user = (Tbmuser) session.createCriteria(Tbmuser.class)
				.add(Restrictions.eq("userId", username)).uniqueResult();

		String emailUser = null;
		String password;
		String passwordDecript;
		String subject = "Pemberitahuan password untuk login ke Sistem Informasi Akademik UIN Jakarta";
		String body = null;
		String sender = "puskom@uinjkt.ac.id";

		if (user != null) {
			if (user.getEmail() == null) {
				Messagebox
						.show("Email anda belum terdaftar, silahkan hubungi admin untuk memasukkan email anda",
								"Peringatan", Messagebox.OK, Messagebox.ERROR);
				return;
			}
			emailUser = user.getEmail();
			password = user.getUserPassword();
			passwordDecript = Common.desEncrypter.decrypt(password);
			body = "Username anda : " + user.getUserId() + " . Password : "
					+ passwordDecript;

			MailSender.sendMail(subject, body, sender, emailUser);
			Messagebox.show("Password anda telah dikirim ke email anda ("
					+ emailUser + "), silahkan cek email anda", "Peringatan",
					Messagebox.OK, Messagebox.EXCLAMATION);
		}

	}
}
